/**
 * Created by fangjiahui on 15/12/4.
 */
// 所有模块都通过 define 来定义
define(function (require, exports, module) {

    $('input[name="findCode"]').on('click', function (element) {
        $(".findCodeLayer").slideUp(0);
        if ($('input[name="findCode"]:checked').val() == 0) {
            $(".findCodePhone").slideDown();
        }
        if ($('input[name="findCode"]:checked').val() == 1) {
            $(".findCodeMail").slideDown();
        }

    });
    
    $("#getPhoneCodeEdit").click(function(){
    	if(!/^1[3578]\d{9}$/.test($("#sendMobileIpt").val())){
			layer.alert("请先填写手机号码");
			return false;
		}
		var _that = this;
		$.post('../../app/user/getPwdCode.html?mobilePhone='+$("#sendMobileIpt").val(), function(data) {
			if(data.result==true){
				get_code_time(_that);				
				layer.alert("发送成功");
			}else{
				layer.alert("发送失败，请稍后再试");
			}
		});
    });
    
    $("#getEmailCodeEdit").click(function(){
    	if(!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|)+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d)|(([a-z]|\d)([a-z]|\d|-|\.|_|~)*([a-z]|\d)))\.)+(([a-z])|(([a-z])([a-z]|\d|-|\.|_|~)*([a-z])))$/i.test($("#sendEmailIpt").val())){
			layer.alert("请输入正确的邮箱地址");
			return;
		}
		var _that = this;
		$.post('../../app/user/getPwdCode.html?email='+$("#sendEmailIpt").val(), function(data) {
			if(data.result==true){
				get_code_time(_that);				
				layer.alert("发送成功");
			}else{
				layer.alert("发送失败，请稍后再试");
			}
		});
    });

    $("#getPwdByMobileFirst").validate({
		rules : {
			'mobilePhone' : {
				required : true,
				isMobile:true
			},
            'validcode':{
            	required: true
            }
		},
		messages : {
			'mobilePhone' : {
				required:"请输入手机号码",
				isMobile:"手机号码格式错误",
				remote:"该手机号码不存在"
			},
            'valicode':{
            	required: '请输入验证码'
            }
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
					$(form).slideUp();
            		$("#getPwdByMobileSec").slideDown();	
            		$("#sendMobile").text($("#mobilePhone").val());
            		$("#sendMobileIpt").val($("#mobilePhone").val());
				} else {
					$('.vaildimg').attr('src','/validimg.html?t=' + Math.random());
					layer.alert(data.resMsg);
				}
			});
		}
	});
    
    $("#getPwdByMobileSec").validate({
    	rules : {			
			'code': {
                required: true
            }
		},
		messages : {			
			'code': {
                required: "请输入手机验证码"
            }
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
					$(form).slideUp();
            		$("#getPwdByMobileThird").slideDown();	
            		$("#mobilePhoneThird").val(data.mobilePhone);           		
				} else {
					layer.alert(data.resMsg);
				}
			});
		}
    });
    
    
    $("#getPwdByMobileThird").validate({
    	rules : {			
    		'pwd' : {
				required : true,
				regexPassword:true
			},
			'confirmNewPwd' : {
				required : true,
				minlength : 5,
				equalTo : "#pwd"
			}
		},
		messages : {			
			'pwd' : {
				required: "8-16位字符，其中包括至少一个字母和一个数字" ,
				regexPassword:"密码格式错误"
			},
			'confirmNewPwd' : {
				required: "请输入确认密码",
				equalTo: "两次输入密码不一致"
			}
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
					$(form).slideUp();
            		$("#getPwdByMobileForth").slideDown();	  
            		$("#usernameForth").text(data.userName);
				} else {
					layer.alert(data.resMsg);
				}
			});
		}
    });
    
    
    $("#getPwdByEmailFirst").validate({
		rules : {
			'email' : {
				required : true,
				email:true
			},
            'validcode':{
            	required: true
            }
		},
		messages : {
			'mobilePhone' : {
				required:"请输入邮箱地址",
				isMobile:"邮箱地址格式错误",
				remote:"该邮箱地址不存在"
			},
            'valicode':{
            	required: '请输入验证码'
            }
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
					$(form).slideUp();
            		$("#getPwdByEmailSec").slideDown();	
            		$("#sendEmail").text($("#email").val());
            		$("#sendEmailIpt").val($("#email").val());
				} else {
					layer.alert(data.resMsg);
				}
			});
		}
	});
    
    $("#getPwdByEmailSec").validate({
    	rules : {			
			'code': {
                required: true
            }
		},
		messages : {			
			'code': {
                required: "请输入邮箱验证码"
            }
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
					$(form).slideUp();
            		$("#getPwdByEmailThird").slideDown();	
            		$("#emailThird").val(data.email);           		
				} else {
					layer.alert(data.resMsg);
				}
			});
		}
    });
    
    
    $("#getPwdByEmailThird").validate({
    	rules : {			
    		'pwd' : {
				required : true,
				regexPassword:true
			},
			'confirmNewPwd' : {
				required : true,
				minlength : 5,
				equalTo : "#emailpwd"
			}
		},
		messages : {			
			'pwd' : {
				required: "8-16位字符，其中包括至少一个字母和一个数字" ,
				regexPassword:"密码格式错误"
			},
			'confirmNewPwd' : {
				required: "请输入确认密码",
				equalTo: "两次输入密码不一致"
			}
		},
		submitHandler : function(form, event, validator) {		
			$(form).ajaxSubmit(function(data) {				
				var result = data.resCode;
				if (result == 1) {
					$(form).slideUp();
            		$("#getPwdByEmailForth").slideDown();	  
            		$("#usernameEForth").text(data.userName);
				} else {
					layer.alert(data.resMsg);
				}
			});
		}
    });
    
    
    
    var wait = 60;
	get_code_time = function (elem) {
		if (wait == 0) {
			$(elem).text("重新发送");
			$(elem).next().hide();
			$(elem).show();
			wait = 60;
		} else {
			$(elem).next().text("("+wait+")秒后重新获取");
			$(elem).hide();
			$(elem).next().show();
			wait--;
			setTimeout(function() {
				get_code_time(elem);
			}, 1000);
		}
	};

});